package com.relife.core.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

@Repository
public class Jdbc {
	@Autowired
	@Qualifier("smsDataSource")
	private DruidDataSource dataSource;

	public int execute(String sql, Object... objs) {
		int ret = 0;
		DruidPooledConnection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(sql.toString());
			if (objs != null) {
				for (int i = 0; i < objs.length; i++) {
					stmt.setObject(i + 1, objs[i]);
				}
			}
			ret = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
}
